package com.mes.task.domain;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.JoinColumnOrFormula;
import org.hibernate.annotations.JoinColumnsOrFormulas;
import org.hibernate.annotations.JoinFormula;
import org.hibernate.annotations.Where;
import org.springframework.context.annotation.Lazy;

import com.mes.basicdata.domain.Device;
import com.mes.basicdata.domain.Employee;
import com.mes.basicdata.domain.TypesData;
import com.mes.common.domain.BasicModel;

/**
 * <p>
 * AssignmentDevice实体,指派到设备.
 * </p>
 * 
 */
@Entity()
// @org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name = "T_ASSIGNMENTS")
@Where(clause = "assntype = 0")
@BatchSize(size = 20)
@Lazy(value = true)
// @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AssignmentDevice extends BasicModel {
	public final static int ASSNSTATE_CREATED = 0; // 已创建
	public final static int ASSNSTATE_STARTED = 1; // 已下达
	public final static int ASSNSTATE_FINISHED = 2; // 已完成
	public final static int ASSNSTATE_CANCELED = 4; // 已取消

	@Id
	@GenericGenerator(name = "uniqueId", strategy = "assigned")
	@Column(name = "UNIQUEID", unique = true)
	protected String uniqueId;

	@Column(name = "TASKUID")
	protected String taskUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "TASKUID", insertable = false, updatable = false)
	protected Task task;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "TASKUID", insertable = false, updatable = false)
	protected OperationTask operationTask;

	// 分派的员工
	@Column(name = "EMPLOYEEID")
	protected String employeeUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "EMPLOYEEID", insertable = false, updatable = false)
	protected Employee employee;

	/**
	 * 类型与状态映射--assntype
	 */
	@Column(name = "ASSNTYPE")
	protected Integer assnType;
	// // many-to-one
	// @ForeignKey(name = "null")
	// @ManyToOne(fetch = FetchType.LAZY)
	// @JoinColumnsOrFormulas(value = {
	// @JoinColumnOrFormula(column = @JoinColumn(name = "MANHOURSTATE",
	// referencedColumnName = "ID", nullable = true, insertable = false,
	// updatable = false)),
	// @JoinColumnOrFormula(formula = @JoinFormula(value = "'assntype'",
	// referencedColumnName = "TYPESNAME")) })
	// protected TypesData assnTypeDesc;

	@Column(name = "ASSNSTART")
	protected Date assnStart;
	@Column(name = "ASSNFINISH")
	protected Date assnFinish;
	@Column(name = "ASSNQTY")
	protected Double assnQty;

	/**
	 * 分派单元数，可以为百分比
	 */
	@Column(name = "ASSNUNITS")
	protected Double assnUnits;
	/**
	 * 类型与状态映射--assnstate
	 */
	@Column(name = "ASSNSTATE")
	protected Integer assnState;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumnsOrFormulas(value = {
			@JoinColumnOrFormula(column = @JoinColumn(name = "MANHOURSTATE", referencedColumnName = "ID", nullable = true, insertable = false, updatable = false)),
			@JoinColumnOrFormula(formula = @JoinFormula(value = "'assnstate'", referencedColumnName = "TYPESNAME")) })
	protected TypesData assnStateDesc;

	/**
	 * 是否有效
	 */
	@Column(name = "ISINEFFECT")
	protected Boolean effective;

	@Column(name = "ASSNWORK")
	protected Double assnWork;
	/**
	 * 实际开始时间
	 */
	@Column(name = "ACTUALSTART")
	protected Date actualStart;
	/**
	 * 实际结束时间
	 */
	@Column(name = "ACTUALFINISH")
	protected Date actualFinish;

	/**
	 * 已完成工时
	 */
	@Column(name = "COMPLETEWORK")
	protected Double completeWork;
	/**
	 * 已完成数量
	 */
	@Column(name = "COMPLETEQTY")
	protected Double completeQty;
	/**
	 * 实际工时
	 */
	@Column(name = "ACTUALWORK")
	protected Double actualWork;

	/**
	 * 应废除字段
	 */
	@Column(name = "DISPATCHTYPE")
	protected String dispatchType;

	@Column(name = "RESUID")
	protected String deviceUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "RESUID", insertable = false, updatable = false)
	protected Device device;

	/*
	 * 该派工项对应的加工记录
	 */
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "assignmentDevice")
	protected Set<WorkRecord> workRecords;

	@Column(name = "PREOPTIME")
	protected Double preopTime;
	@Column(name = "RUNTIME")
	protected Double runTime;

	/**
	 * 空的构造函数
	 */
	public AssignmentDevice() {
		this.setAssnType(Assignment.ASSNTYPE_DEVICE);
	}

	public Double getPreopTime() {
		return preopTime;
	}

	public void setPreopTime(Double preopTime) {
		this.preopTime = preopTime;
	}

	public Double getRunTime() {
		return runTime;
	}

	public void setRunTime(Double runTime) {
		this.runTime = runTime;
	}

	/**
	 * 获取设备Uid
	 * 
	 * @return
	 */
	public String getDeviceUid() {
		return deviceUid;
	}

	/**
	 * 设置设备Uid
	 * 
	 * @param deviceUid
	 */
	public void setDeviceUid(String deviceUid) {
		this.deviceUid = deviceUid;
	}

	/**
	 * 获取设备
	 * 
	 * @return
	 */
	public Device getDevice() {
		return device;
	}

	/**
	 * 设置设备
	 * 
	 * @param device
	 */
	public void setDevice(Device device) {
		this.device = device;
	}

	public Set<WorkRecord> getWorkRecords() {
		return workRecords;
	}

	public void setWorkRecords(Set<WorkRecord> workRecords) {
		this.workRecords = workRecords;
	}

	@Override
	public String getUuid() {
		// TODO Auto-generated method stub
		return this.getUniqueId();
	}

	@Override
	public void setUuid(String uuid) {
		// TODO Auto-generated method stub
		this.setUniqueId(uuid);
	}

	public String getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(String uniqueId) {
		this.uniqueId = uniqueId;
	}

	public String getTaskUid() {
		return taskUid;
	}

	public void setTaskUid(String taskUid) {
		this.taskUid = taskUid;
	}

	public OperationTask getOperationTask() {
		return operationTask;
	}

	public void setOperationTask(OperationTask operationTask) {
		this.operationTask = operationTask;
	}

	public String getEmployeeUid() {
		return employeeUid;
	}

	public void setEmployeeUid(String employeeUid) {
		this.employeeUid = employeeUid;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Integer getAssnType() {
		return assnType;
	}

	public void setAssnType(Integer assnType) {
		this.assnType = assnType;
	}

	public Date getAssnStart() {
		return assnStart;
	}

	public void setAssnStart(Date assnStart) {
		this.assnStart = assnStart;
	}

	public Date getAssnFinish() {
		return assnFinish;
	}

	public void setAssnFinish(Date assnFinish) {
		this.assnFinish = assnFinish;
	}

	public Double getAssnQty() {
		return assnQty;
	}

	public void setAssnQty(Double assnQty) {
		this.assnQty = assnQty;
	}

	public Double getAssnUnits() {
		return assnUnits;
	}

	public void setAssnUnits(Double assnUnits) {
		this.assnUnits = assnUnits;
	}

	public Integer getAssnState() {
		return assnState;
	}

	public void setAssnState(Integer assnState) {
		this.assnState = assnState;
	}

	public TypesData getAssnStateDesc() {
		return assnStateDesc;
	}

	public void setAssnStateDesc(TypesData assnStateDesc) {
		this.assnStateDesc = assnStateDesc;
	}

	public Boolean getEffective() {
		return effective;
	}

	public void setEffective(Boolean effective) {
		this.effective = effective;
	}

	public Double getAssnWork() {
		return assnWork;
	}

	public void setAssnWork(Double assnWork) {
		this.assnWork = assnWork;
	}

	public Date getActualStart() {
		return actualStart;
	}

	public void setActualStart(Date actualStart) {
		this.actualStart = actualStart;
	}

	public Date getActualFinish() {
		return actualFinish;
	}

	public void setActualFinish(Date actualFinish) {
		this.actualFinish = actualFinish;
	}

	public Double getCompleteWork() {
		return completeWork;
	}

	public void setCompleteWork(Double completeWork) {
		this.completeWork = completeWork;
	}

	public Double getCompleteQty() {
		return completeQty;
	}

	public void setCompleteQty(Double completeQty) {
		this.completeQty = completeQty;
	}

	public Double getActualWork() {
		return actualWork;
	}

	public void setActualWork(Double actualWork) {
		this.actualWork = actualWork;
	}

	public String getDispatchType() {
		return dispatchType;
	}

	public void setDispatchType(String dispatchType) {
		this.dispatchType = dispatchType;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

}
